package com.sg.business.permission.adapter.visitor;

import org.eclipse.core.runtime.IAdapterFactory;

import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.AbstractRoleAssignment;
import com.sg.business.model.DummyModel;
import com.sg.business.model.Organization;
import com.sg.business.model.ProjectRole;
import com.sg.business.model.Role;
import com.sg.business.model.RoleDefinition;
import com.sg.business.model.User;
import com.sg.business.model.toolkit.UserToolkit;
import com.sg.business.permission.model.IPermissionVisitor;
import com.sg.business.permission.model.PermissionVisitor;

public class VisitorAdapter implements IAdapterFactory {

	@SuppressWarnings("rawtypes")
	@Override
	public IPermissionVisitor getAdapter(Object adaptableObject,
			Class adapterType) {
		if (adaptableObject instanceof PrimaryObject) {
			if (adaptableObject instanceof DummyModel) {
				String type = (String) ((PrimaryObject) adaptableObject)
						.getValue(IPermissionVisitor.F_VISITOR_TYPE);
				String name = (String) ((PrimaryObject) adaptableObject)
						.getValue(IPermissionVisitor.F_VISITOR_NAME);
				Object value = ((PrimaryObject) adaptableObject)
						.getValue(IPermissionVisitor.F_VISITOR);
				PermissionVisitor visitor = new PermissionVisitor(value, type,
						name);
				return visitor;
			} else if (adaptableObject instanceof ProjectRole) {
				ProjectRole projectRole = (ProjectRole) adaptableObject;
				if (projectRole.isOrganizatioRole()) {
					Role orgRole = projectRole.getOrganizationRole();
					if (orgRole != null) {
						return new RoleVisitorFactory(orgRole).createVisitor();
					}
				} else {
					return new ProjectRoleVisitorFactory(projectRole)
							.createVisitor();
				}
			} else if (adaptableObject instanceof AbstractRoleAssignment) {
				AbstractRoleAssignment assignment = (AbstractRoleAssignment) adaptableObject;
				String userId = assignment.getUserid();
				User user = UserToolkit.getUserById(userId);
				if (user != null) {
					return new UserVisitorFactory(user).createVisitor();
				}
			} else if (adaptableObject instanceof RoleDefinition) {
				RoleDefinition roleDefinition = (RoleDefinition) adaptableObject;
				if (roleDefinition.isOrganizatioRole()) {
					Role orgRole = roleDefinition.getOrganizationRole();
					if (orgRole != null) {
						return new RoleVisitorFactory(orgRole).createVisitor();
					}
				} else {
					return new ProjectTemplateRoleVisitorFactory(roleDefinition)
							.createVisitor();
				}
			}else if(adaptableObject instanceof User){
				return new UserVisitorFactory((User) adaptableObject).createVisitor();
			}else if(adaptableObject instanceof Role){
				return new RoleVisitorFactory((Role) adaptableObject).createVisitor();
			}else if(adaptableObject instanceof Organization){
				return new OrgVisitorFactory((Organization) adaptableObject).createVisitor();
			}
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Class[] getAdapterList() {
		// return new Class[] { OrgVisitorFactory.class,
		// RoleVisitorFactory.class,
		// ProjectRoleVisitorFactory.class, UserVisitorFactory.class,
		// StaticVisitor.class };
		return new Class[] { IPermissionVisitor.class };
	}

}
